/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package XMLParsing;

import java.util.ArrayList;
import java.util.Iterator;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.File;
import java.io.FileOutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import semiSupervise.Ensemble;

/**
 *
 * @author mida
 */
public class PartitionFileCreator {

    public static void createDoc(ArrayList<ArrayList<Object>> classes) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        Document doc = null;
        Element root = null, grp, cls;
        XMLSerializer xmlSer;
        OutputFormat out;

        //Try to create the document builder
        try {
            db = dbf.newDocumentBuilder();
            doc = db.newDocument();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        //Creat the root class, called TEst
        root = doc.createElement("ontologie");
        doc.appendChild(root);

        Iterator<ArrayList<Object>> itr = classes.iterator();
        int i = 0;
        while (itr.hasNext()) {
            //Create child node to root
            grp = doc.createElement("groupe");
            grp.setAttribute("num", String.valueOf(++i));
            root.appendChild(grp);
            int k = 0;
            ArrayList<Object> classe = itr.next();
            while (k < classe.size()) {
                cls = doc.createElement("classe");
                cls.setTextContent(classe.get(k).toString());
                grp.appendChild(cls);
                ++k;
            }
        }

        //Write out to file using the serializer
        try {
            out = new OutputFormat(doc);
            out.setIndenting(true);

            xmlSer = new XMLSerializer(
                    new FileOutputStream(new File("C:/Users/mida/Desktop/class.xml")), out);
            xmlSer.serialize(doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createXML(ArrayList<Ensemble> classes) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        Document doc = null;
        Element root = null, grp, cls;
        XMLSerializer xmlSer;
        OutputFormat out;

        //Try to create the document builder
        try {
            db = dbf.newDocumentBuilder();
            doc = db.newDocument();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        //Creat the root class, called TEst
        root = doc.createElement("ontologie");
        doc.appendChild(root);

        Iterator<Ensemble> itr = classes.iterator();
        int i = 0;
        while (itr.hasNext()) {
            //Create child node to root
            grp = doc.createElement("groupe");
            grp.setAttribute("num", String.valueOf(++i));
            root.appendChild(grp);
            int k = 0;
            Ensemble classe = itr.next();
            while (k < classe.size()) {
                cls = doc.createElement("classe");
                cls.setTextContent(classe.get(k).toString());
                grp.appendChild(cls);
                ++k;
            }
        }

        //Write out to file using the serializer
        try {
            out = new OutputFormat(doc);
            out.setIndenting(true);

            xmlSer = new XMLSerializer(
                    new FileOutputStream(new File("C:/Users/mida/Desktop/class.xml")), out);
            xmlSer.serialize(doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
